{% extends "master.html" %}
{% load common_extras %}
{% block head_ext %}
<script type="text/javascript" src="/site_media/js/rpc.js"></script>
<script type="text/javascript" src="/site_media/js/jquery/ui/ui.core.js"></script>
<script type="text/javascript" src="/site_media/js/jquery/ui/ui.dialog.js"></script>
<link rel="stylesheet" href="/site_media/js/jquery/ui/themes/flora/flora.all.css" type="text/css" media="screen" title="no title" />
<meta http-equiv="content-type" content="text/html; charset=utf8" />
<script type="text/javascript">
     
  service = new rpc.ServiceProxy("/services/admin/", {asynchronous:false, methods: ['add_user','edit_user','remove_user']});
  
  $(document).ready(function(){
	genPageLinks({{total}}, {{pagesize}});
	
	$('input.add').click(function(){
	     $('#user_id').val(0);
		 $('#username').val("");
		 $('#username').attr("disabled", "");
	     $('#password').val("");
		 $('#nickname').val("");
		 $('#remark').val("");
	     $("#add_dialog").show().dialog({
		     height: 230, width: 300, modal: true,
	         title: '增加用户',
		     overlay: {
                opacity: 0.5, 
                background: "black" 
             }
	     });
	});
	
    $('a.edit').click(function(){
	     var id = $(this).parent().parent().attr('id');
		 var username = $(this).parent().parent().attr('username');
		 var password = $(this).parent().parent().attr('password');
		 var nickname = $(this).parent().parent().attr('nickname');
		 var remark = $(this).parent().parent().attr('remark');
	     $('#user_id').val(id);
		 $('#username').val(username);
	     $('#password').val(password);
		 $('#nickname').val(nickname);
		 $('#remark').val(remark);
		 
		 $('#username').attr("disabled", "disabled");

	     $("#add_dialog").show().dialog({
		     height: 230, width: 350, modal: true,
	         title: '修改用户',
		     overlay: {
                opacity: 0.5, 
                background: "black" 
             }
	     });
	});
	
	$('a.delete').click(function(){
	     
	     var id = $(this).parent().parent().attr('id');
		 var nickname = $(this).parent().parent().attr('nickname');
		 var message = "是否将用户[" + nickname + "]删除 ?";
		 if(confirm(message)){
		     try{
		         service.remove_user(id);
			     window.location.reload();
		     }catch(error){
			     alert(error.message);
			 }
		 }
	});
	
	$('#add_form input.button').click(function(){
	    var user_id = $('#user_id').val();
		var username = $('#username').val();
	    var password = $('#password').val();
		var nickname = $('#nickname').val();
		var remark = $('#remark').val();
		
	    if(!username){
		   alert("请输入用户名");
		}else{
		    try{
		       if(user_id == 0){
		    	   service.add_user({'id': user_id, 'username': username, 'password': password, 'nickname': nickname, 'remark': remark});
		       }
		       else{ 
		    	   service.edit_user({'id': user_id, 'username': username, 'password': password, 'nickname': nickname, 'remark': remark});
		       }
	           window.location.reload() ;
			}catch(e){
			   alert(e.message);
			}
	    }
	});
	
  });
  
</script>
{% endblock %}
{% block title %}我的邀请历史{% endblock %}
{% block content %}
<input type="button" class="add" value="添加用户"/>
<table cellspacing="1" class="tablesorter">
  <thead>
    <th>用户ID</th>
    <th>用户名</th>
    <th>昵称</th>
    <th>备注</th>
    <th>修改 </th>
	<th>删除</th>
    <th>权限设置</th>
  </thead>
  <tbody>
    {% if infos %}
      {% for info in infos %}
       <tr id="{{info.id}}" username="{{info.username}}" password="{{info.password}}" nickname="{{info.nickname}}" remark="{{info.remark}}">
         <td>{{ info.id }}</td>
         <td>{{ info.username }}</td>
         <td>{{ info.nickname}}</td>
         <td>{{ info.remark}}</td>
         <td><a href="#" class="edit">编辑</a></td>
		 <td><a href="#" class="delete">删除</a></td>
		 <td><a href="/admin/prlist/?id={{info.id}}" class="privilege">权限设置</a></td>
       </tr>
      {% endfor %}
    {% endif %}
     <tr style="display:none"><td id="pageLink" colspan="12"></td></tr>
  </tbody>
</table>
<div id="add_dialog" class="flora" style="display: none;">
    <form id="add_form">
    	<p>
    	    <input id="user_id" type="hidden" value="0"/>
            <span>用户名:<input id="username" type="text" value="" size="30" /></span><br>
			<br>
			<span>密&nbsp;&nbsp;码:<input id="password" type="password" value="" size="30" value="0"/></span><br><br>
			<span>昵&nbsp;&nbsp;称:<input id="nickname" type="text" value="" size="30" value="0"/></span><br><br>
			<span>备&nbsp;&nbsp;注:<input id="remark" type="text" value="" size="30" value="0"/></span><br><br>
			<input class="button" type="button" value="提交" />
		</p>
    </form>
</div>
{% endblock %}